// pages/article/lanmu/index.js
//获取应用实例
const app = getApp()
const util = require("../../../utils/util.js");
var page = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        menuIndex: 0,
        articleList: [],
        lanmuData: [],
        bsylistData: [],

        onelmList: [],
        twolmList: [],

        showLoading: false,
        shownull: false,
        lanmuHeight: 0,
        menuHeight: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        page = 1;
        this.setData({
            classid: options.id ? options.id : '',
            type: options.type ? options.type : '',
            citycode: options.city ? options.city : (ks.getStorageSync("citycode") ? ks.getStorageSync("citycode") : 'sz'),
            menuIndex: options.menuIndex ? options.menuIndex : 0,
            // name: options.name ? decodeURIComponent(options.name):""
        });

        util.getUrl(app.globalData.url).then((res) => {
            this.getbsylist();
            this.getArticleInfo(this.data.classid, 1)
        })
    },

    // 办事频道
    getbsylist() {
        let data = {
            citycode: this.data.citycode == "sz" ? "bsy" : this.data.citycode,
            action: "sitelist",
            pagesize: 4,
            bsy: 1,
            version: app.globalData.version,
        };
        util.request('index.php', data, 'GET').then(res => {
            if (res.data && Array.isArray(res.data)) {
                this.setData({
                    bsylistData: res.data,
                })

                res.data.forEach((v, k) => {
                    if (v.id == this.data.classid) {
                        this.setData({
                            pindex: k,
                        })
                        this.getOnelm(this.data.classid)
                    }
                })
                setTimeout(res => {
                    var query = ks.createSelectorQuery()
                    query.select('.scolls').boundingClientRect((res) => {
                        this.setData({
                            menuHeight: (res != null) ? res.height + 10 : 0,
                        });
                    }).exec()
                }, 100)
            }
        })
    },

    // 获取一级子栏目列表
    getOnelm(classid) {
        let lanmuData = {
            action: "classlist",
            citycode: this.data.type == 'wenda' ? 'wenda' : ((this.data.type == 'bsy' && this.data.citycode == 'sz') ? 'bsy' :
                this.data.citycode),
            classid,
        };
        util.request('index.php', lanmuData, 'GET').then(res => {
            if (res.data) {
                res.data.forEach((v, k) => {
                    v.checked = false;
                })
                this.setData({
                        onelmList: res.data,
                    })
                    // this.onelmList = res.data;
            }
            setTimeout(res => {
                var query = ks.createSelectorQuery();
                if (this.data.onelmList)
                    query.select('.lanmus').boundingClientRect((res) => {
                        this.setData({
                            lanmuHeight: (res != null) ? res.height : 0,
                        });
                    }).exec()
            }, 100)
        })
    },

    // 获取栏目文章列表
    getArticleInfo(classid, page) {
        let infoData = {
            citycode: this.data.type == 'wenda' ? 'wenda' : ((this.data.type == 'bsy' && this.data.citycode == 'sz') ? 'bsy' :
                this.data.citycode),
            action: 'list',
            // city: this.data.city,
            pagesize: 10,
            classid,
            page,
            // timestamp: new Date().getTime(),
        };
        util.request('index.php', infoData, 'GET').then(res => {
            // if (res.data == "") {
            //   this.requestStatus = "noMore";
            // }
            this.setData({
                articleList: this.data.articleList.concat(res.data),
            })

            // if (this.articleList.length < 15) {
            //   this.requestStatus = "noMore";
            // }
            this.setData({
                showLoading: false,
            })

            if (this.data.articleList == "") {
                this.setData({
                    shownull: true,
                })
            }
        })
    },

    // 一级栏目点击
    menuClick(e) {
        let id = e.currentTarget.dataset.id;
        let classname = e.currentTarget.dataset.classname;
        let index = e.currentTarget.dataset.index;
        let pagetype = e.currentTarget.dataset.pagetype;
        this.setData({
                menuIndex: index,
            })
            // this.menuIndex = index;
        this.data.onelmList.forEach((v, k) => {
            if (index == k) {
                v.checked = true;
            } else {
                v.checked = false;
            }
        })

        this.setData({
                onelmList: this.data.onelmList,
                articleList: [],
                classid: id,
                type: pagetype,
                classname: classname,
                showLoading: true,
                shownull: false,
            })
            // this.getTwolm(item.id)
        this.getOnelm(id)
        this.getArticleInfo(id, 1)
    },

    // 二级栏目点击
    lanmuClick(e) {
        let id = e.currentTarget.dataset.id;
        let classname = e.currentTarget.dataset.classname;
        let index = e.currentTarget.dataset.index;
        let pagetype = e.currentTarget.dataset.pagetype;
        this.data.onelmList.forEach((v, k) => {
            if (index == k) {
                v.checked = true;
            } else {
                v.checked = false;
            }
        })
        this.setData({
            onelmList: this.data.onelmList,
            articleList: [],
            classid: id,
            type: pagetype,
            classname: classname,
            showLoading: true,
            shownull: false,
        })
        this.getArticleInfo(id, 1)
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        page++;
        this.getArticleInfo(this.data.classid, page)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})